package cn.szzsi._webpc;

import cn.szzsi._web.BaseWebController;
import cn.szzsi.common.model.Member;
import cn.szzsi.common.model.Receiver;
import cn.szzsi.validator.ReceiverValidator;
import com.jfinal.aop.Before;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 前台类 - 收货地址
 *
 */
public class ReceiverController extends BaseWebPcController<Receiver> {

	private Receiver receiver;
	private String path;//模板路径
	private String shopId;//用户的shopId就是商户的id
	// 收货地址列表
	public void list() {
		shopId=getLoginMember().getStr("shopId");//用户的shopId对应商户的id(uid),商户的shopId为0
		String returnUrl=getPara("returnUrl");
		setAttr("receiver", Receiver.dao.getReceiverList(getLoginMember()));
		setAttr("returnurl","returnurl");
		setAttr("from","conform");
		setAttr("useWeixinAddr",true);
		setAttr("returnUrl",returnUrl);
		String productId=getPara("productId");
		if(StrKit.notBlank(productId)){
			setAttr("productId",productId);
		}
		pcTemplatePath("receiver_list.html");

	}

	// 收货地址添加
	public void add() {
		Member loginMember = getLoginMember();
		shopId=getLoginMember().getStr("shopId");//用户的shopId对应商户的id(uid),商户的shopId为0
		List<Receiver> receiverList = loginMember.getReceiverList();
		if (receiverList != null && Receiver.MAX_RECEIVER_COUNT != null && receiverList.size() >= Receiver.MAX_RECEIVER_COUNT) {
			addActionError("只允许添加最多" + Receiver.MAX_RECEIVER_COUNT + "项收货地址!");
			return;
		}
		pcTemplatePath("receiver_input.html");
	}

	// 获取单个地址实例
	public void get() {
		String id=getPara("id");
		Receiver r = Receiver.dao.findById(id);
		renderJson(r);
	}

	// 收货地址编辑
	public void edit() {
		shopId=getLoginMember().getStr("shopId");//用户的shopId对应商户的id(uid),商户的shopId为0
		id = getPara("id","");
		if (StrKit.notBlank(id)){
			receiver = Receiver.dao.findById(id);
		}
		if(receiver.equals(getLoginMember().getStr("id"))) {
			addActionError("参数错误!");
			return;
		}
		setAttr("receiver", receiver);
		pcTemplatePath("receiver_input.html");
	}

	// 收货地址删除
	public void delete() {
		id = getPara("id","");
		Receiver receiver = Receiver.dao.findById(id);
		if(receiver.equals(getLoginMember().getStr("id"))) {
			addActionError("参数错误!");
			return;
		}
		receiver.delete();
		Map<String, String> jsonMap = new HashMap<String, String>();
		jsonMap.put(STATUS, SUCCESS);
		renderJson(jsonMap);
	}

	// 收货地址删除
	public void pcDelete() {
		id = getPara("id","");
		Receiver receiver = Receiver.dao.findById(id);
		if(receiver.equals(getLoginMember().getStr("id"))) {
			addActionError("参数错误!");
			return;
		}
		receiver.delete();
		redirect("/b/receiver/list");
	}

	// 设置默认地址
	public void setdefault() {
		String memberId=getLoginMember().getStr("id");
		id = getPara("id","");
		Db.update("update mall_receiver set isDefault = 0 where member_id=?",memberId);
		Db.update("update mall_receiver set isDefault = 1 where id = ?",id);
		Map<String, String> jsonMap = new HashMap<String, String>();
		jsonMap.put(STATUS, SUCCESS);
		renderJson(jsonMap);
	}

	// 收货地址保存
	@Before(ReceiverValidator.class)
	public void save() {
		receiver = getModel(Receiver.class);
		Member loginMember = getLoginMember();
		List<Receiver> receiverList = loginMember.getReceiverList();
		if (receiverList != null && Receiver.MAX_RECEIVER_COUNT != null && receiverList.size() >= Receiver.MAX_RECEIVER_COUNT) {
			addActionError("只允许添加最多" + Receiver.MAX_RECEIVER_COUNT + "项收货地址!");
			return;
		}

		receiver.set("member_id", loginMember.getStr("id"));
		if(StrKit.isBlank(receiver.getStr("id"))){
			receiver.save(receiver,loginMember.getStr("id"));
		}else{
			receiver.update(receiver,loginMember.getStr("id"));
		}
		Map<String, String> jsonMap = new HashMap<String, String>();
		jsonMap.put(STATUS, SUCCESS);
		renderJson(jsonMap);
//		redirect("/receiver/list");
	}

	// 收货地址保存
	@Before(ReceiverValidator.class)
	public void pcSave() {
		shopId=getLoginMember().getStr("shopId");//用户的shopId对应商户的id(uid),商户的shopId为0
		receiver = getModel(Receiver.class);
		String areaPath=receiver.getProvince()+" "+receiver.getCity()+" "+receiver.getArea();
		if (StrKit.isBlank(receiver.getPhone()) && StrKit.isBlank(receiver.getMobile())) {
			addActionError("联系电话、联系手机必须填写其中一项!");
			return;
		}
		Member loginMember = getLoginMember();
		List<Receiver> receiverList = loginMember.getReceiverList();
		if (receiverList != null && Receiver.MAX_RECEIVER_COUNT != null && receiverList.size() >= Receiver.MAX_RECEIVER_COUNT) {
			addActionError("只允许添加最多" + Receiver.MAX_RECEIVER_COUNT + "项收货地址!");
			return;
		}

		receiver.set("member_id", loginMember.getStr("id"));
		receiver.set("areaPath",areaPath);
		receiver.save(receiver,getLoginMember().getStr("id"));
		redirect("/b/receiver/list");
	}

	// 收货地址更新
	public void update() {
		receiver = getModel(Receiver.class);
		if (StrKit.isBlank(receiver.getStr("phone")) && StrKit.isBlank(receiver.getStr("mobile"))) {
			addActionError("联系电话、联系手机必须填写其中一项!");
			return;
		}
		Receiver persistent = Receiver.dao.findById(receiver.getStr("id"));
		if(persistent.equals(getLoginMember().getStr("id"))) {
			addActionError("参数错误!");
			return;
		}
		receiver.update(receiver,getLoginMember().getStr("id"));
		redirect("/b/receiver/list");
	}


}
